<?php // $Id: article_lister.php 279 2008-08-13 22:33:46Z pcoustillas $ ?>
<?php
/*
Copyright (C) 2008-2009 Pierre Coustillas
Licensed under the terms of the GNU  General Public License:http://www.opensource.org/licenses/gpl-license.php
File Authors:Pierre Coustillas
*/
require_once("include/verif.php");
require_once("include/config/common.php");
require_once("include/config/var.php");
require_once("include/language/$lang.php");
require_once("include/utils.php");
require_once("include/headers.php");

require_once("include/categorie_choix.php");

?>

<script language="javascript" src="javascripts/confdel.js"  type="text/javascript" ></script>
<script language="javascript" src="javascripts/verifier_form.js" type="text/javascript"></script>
<script type="text/javascript" src="javascripts/tri_formulaire.js"></script>
<script type="text/javascript">
function change_prix(ht_ou_ttc_ou_tva)
{
//cette fonction calcul en auto le prix TTC ou HT en fonction du dernier modifiÃ©
//seul le pric HT a une importance pour le formulaire
//si le prix saisie contient une virgule, elle est d'abord transformé en point.
	document.article.prix_ttc.value=document.article.prix_ttc.value.replace(",",".");
	document.article.prix.value=document.article.prix.value.replace(",",".");
	document.article.taux_tva.value=document.article.taux_tva.value.replace(",",".");

	ttc=document.article.prix_ttc.value;
	ht=document.article.prix.value;
	tva=document.article.taux_tva.value;


	if (ht_ou_ttc_ou_tva == "tva") 
	{
		if (ht != "")
		{
			document.article.prix_ttc.value = Number(ht * tva / 100)  + Number(ht);
		} else {
			if (ttc != "")
			{
			document.article.prix.value = ttc / (1 + tva /100) ;
			}
		}
		

	}
	if (ht_ou_ttc_ou_tva == "prix_ttc")
	{
		document.article.prix.value = ttc / (1 + tva /100) ;
	}
	if (ht_ou_ttc_ou_tva == "prix_ht") 
	{
		document.article.prix_ttc.value = Number(ht * tva / 100)  + Number(ht);
	}

}


function verif_creer_article_stock()
{
	if (verifier('article','black','article','vide','uni','vide','taux_tva','vide#est_nombre','prix','vide#est_nombre','stock','est_nombre','stomin','est_nombre','stomax','est_nombre'))
	{
		document.article.submit();
	} else {
		alert("<?php aff_lang('Une des valeurs saisie n\'est pas correct.\nMerci de corriger avant de poursuivre.') ?>")	
	}

}

function verif_creer_article()
{
	if (verifier('article','black','article','vide','uni','vide','taux_tva','vide#est_nombre','prix','vide#est_nombre'))
	{
		document.article.submit();
	} else {
		alert("<?php aff_lang('Une des valeurs saisie n\'est pas correct.\nMerci de corriger avant de poursuivre.') ?>")	
	}

}
</script>

<?php


?>
<table width="760" border="0" class="page" align="center">
	<tr>
		<td class="page" align="center"  colspan="2">
		<?php
			require_once("include/head.php");
		?>
		</td>
	</tr>
	<tr>
		<td class="page" align="center"  colspan="2">

			<?php
			if ($user_art == "n") {
				echo "<h1>$lang_article_droit</h1>";
				exit;
			}

			if (isset($_GET['message']))
			{
				echo "<h1>" . $_GET['message'] . "</h1>";
			}

			?>

			<form action="article_new.php" method="post" name="article" >
			<center>
				<table>
					<caption>
						<?php aff_lang("Créer un article") ?> &nbsp;  &nbsp;  &nbsp; <?php bouton_aide("article") ?>
					</caption>
					<tr>
						<td>
							<table width="100%">
								<tr>
									<td class="texte0" id="article"><?php echo "$lang_art_no"; ?></td>
									<td class="texte0"><input name="article" type="text"  size="40" maxlength="40" /></td>
								</tr>
								<?php
								if ($conf_xml->categorie->categorie_active=="oui") { ?>
								<tr>
									<td class='<?php echo couleur_alternee (); ?>'><?php echo "$lang_categorie" ?></td> 
									<td class='<?php echo couleur_alternee (FALSE); ?>'>
									<?php
										$rqSql = "SELECT id_cat, categorie FROM ${tblpref}categorie ORDER BY categorie";
										$result = mysql_query( $rqSql ) or die( "Exécution requête impossible."); 
										?>
										<select name='categorie'>
										<?php
										while ($row = mysql_fetch_array( $result))
										{
											?>
											<option value='<?php echo $row["id_cat"] ; ?>'><?php echo $row["categorie"]; ?></option>
											<?php
										}
										?>
										</select>&nbsp; <?php bouton_aide("categorie") ?>
									</td>
								</tr>
								<?php
								} 
								?>
								<tr>
									<td class='<?php echo couleur_alternee (); ?>' id="uni"><?php echo "$lang_uni_art" ?></td>
									<td class='<?php echo couleur_alternee (FALSE); ?>'><input name="uni" type="text" size="8" maxlength="8" value="" /></td>
								</tr>
								<tr>
									<td class='<?php echo couleur_alternee (); ?>' id="taux_tva"><?php echo "$lang_ttva" ?></td>
									<td class='<?php echo couleur_alternee (FALSE); ?>'>
										<input name="taux_tva" type="text" size="5" onchange="change_prix('tva')" maxlength="5" value="0" onchange="virg2pt(this)" /> %
									</td>
								</tr>
								<tr>
									<td class='<?php echo couleur_alternee (); ?>' id="prix"><?php echo "$lang_prixunitaire" ?> HT</td>
									<td class='<?php echo couleur_alternee (FALSE); ?>'>
										<input name="prix" type="text" size="8" onchange="change_prix('prix_ht')" /> &euro;
								&nbsp; TTC <input name="prix_ttc" type="text" size="8" onchange="change_prix('prix_ttc')" />
									</td>
								</tr>

							</table>
						</td>
						<td>
							<table>
								<tr>
									<td class='<?php echo couleur_alternee (); ?>'><?php echo "$langCommentaire" ?> : </td>
									<td class='<?php echo couleur_alternee (FALSE); ?>'><input name="commentaire" type="text" /></td>
								</tr>
							<?php
							if ($conf_xml->stock->stock_actif=="oui") { ?>
								<tr>
									<td colspan="2"><hr/></td>
								</tr>
								<tr>
									<td class='<?php echo couleur_alternee (); ?>' id="stock"><?php echo "$lang_stock"; ?></td>
									<td class='<?php echo couleur_alternee (FALSE); ?>'><input name='stock' type='text' /></td>
								</tr>
								<tr>
									<td class='<?php echo couleur_alternee (); ?>' id="stomin"><?php echo "$lang_stomin"; ?></td>
									<td class='<?php echo couleur_alternee (FALSE); ?>'><input name='stomin' type='text' /></td>
								</tr>
								<tr>
									<td class='<?php echo couleur_alternee (); ?>' id="stomax"><?php echo "$lang_stomax"; ?></td>
									<td class='<?php echo couleur_alternee (FALSE); ?>'><input name='stomax' type='text' /></td>
								</tr>
							<?php } ?>
							</table>
						</td>
					</tr>
					<tr>
						<td class="submit" colspan="2">
							<?php if ($conf_xml->stock->stock_actif=="oui") { ?>
								<input type="button" onclick="verif_creer_article_stock()" value="<?php aff_lang("Créer cet article") ?>" />
							<?php } else { ?>
								<input type="button" onclick="verif_creer_article()" value="<?php aff_lang("Créer cet article") ?>" />
							<?php } ?>
						</td>
					</tr>
				</table>
			</center>
			</form>

		</td>
	</tr>

	<?php
	if ($conf_xml->categorie->categorie_active=="oui") { ?>

	<tr><td  colspan="2"><hr/></td></tr>

	<tr>
		<td style="vertical-align:middle">
			<form action="categorie_new.php" method="post" >
			<center>
				<table>
					<caption>
						<?php aff_lang("Ajouter une catégorie") ?> &nbsp;  &nbsp;  &nbsp; <?php bouton_aide("categorie") ?>
					</caption>
					<tr>
						<td class="texte0" width="50%"> 
							<?php aff_lang("Nom de la catégorie") ?>
						</td>
						<td class="texte0" width="50%">
							<input name="categorie" type="text" size="30" maxlength="30" />
						</td>
					</tr>
					<tr>
						<td class="submit" colspan="2">
							<input type="submit" value="<?php aff_lang("Créer cette catégorie") ?>" />
						</td>
					</tr>
				</table>
			</center>
			</form>

		</td>

		<td>
			<center>
				<table>
					<caption>
						<?php aff_lang("Liste des catégories") ?>
					</caption>
					<tr>
						<th><?php aff_lang("Changer le nom de la catégorie") ?></th>
						<th><?php aff_lang("Nb d'articles") ?></th>
						<th><?php aff_lang("Suppr.") ?></th>
					</tr>
					<?php 
						$sql="SELECT id_cat,categorie,count(cat) as nb_art FROM ${tblpref}categorie 
							LEFT JOIN (SELECT cat FROM ${tblpref}article WHERE actif <> 'non') as art ON id_cat=cat
							GROUP BY id_cat,categorie order by categorie";
						
						$result=mysql_query($sql) or die("erreur select cat<br/>$sql</br/>" . mysql_error());

						$ligne=0;
						while ($data = mysql_fetch_array($result))
						{
							$id_cat=$data['id_cat'];
							$categorie=$data['categorie'];
							$nb_art=$data['nb_art'];

							if ($categorie=='---')
							{
								$disabled="disabled='disabled'";
							} else {
								$disabled="";
							}

						?>
						<tr>
							<td>
								<form name="frm_change_cat" action="categorie_action.php" method="get">
								<input type="hidden" name="action" value="change_nom" />
								<input type="hidden" name="id_cat" value="<?php echo $id_cat ?>" />
								<input name="categorie" <?php echo $disabled ?> type="text" size="30" maxlength="30" value="<?php echo $categorie ?>" />
								<?php if ($disabled=="")
								{ 
								?>
									<input type="image" src="image/ok_kde.png"/></input>
								<?php 
								} 
								?>
								</form>
							</td>
							<td style="text-align:center">
								<?php echo $nb_art ?> 
							</td>
							<td style="text-align:center">
							<?php if ($disabled=="") 
							{ 
							?>
								<a href="categorie_action.php?action=delete&amp;id_cat=<?php echo $id_cat ;?>" onclick="return confirmDelete('<?php aff_lang("Confirmez-vous la suppression de cette catégorie. (ses articles seront déplacer dans la catégorie \'---\')") ?>')"><img border="0" src="image/delete.png" alt="delete cat"/></a>
							<?php
							}
							?>
							</td>
						</tr>


						<?php
						$ligne++;
						}
			

					?>

				</table>
			</center>

		</td>
	</tr>
	
	<?php
	} //fin categorie
	?>

	<tr><td colspan="2"><hr/></td></tr>

	<?php // LISTE DES ARTCILES ?>

	<tr>
		<td colspan="2">
			<?php

			$tri_ordre=isset($_GET['tri_ordre'])?$_GET['tri_ordre']:"ASC"; //ordre de tri
			$tri_champ=isset($_GET['tri_champ'])?$_GET['tri_champ']:"article"; // champ de tri

			$sql = "SELECT * FROM ${tblpref}article LEFT JOIN ${tblpref}categorie ON ${tblpref}categorie.id_cat = " . $tblpref ."article.cat WHERE actif != 'non' ";
			$sql .= " ORDER BY " . $tri_champ . " " . $tri_ordre;
			
			$req = mysql_query($sql) or die("Erreur SQL !<br/>$sql2<br/>".mysql_error());
			?>
			<center>
			<form name="frm_tri" method="get" action="article_lister.php">
				<input type="hidden" name="tri_champ" value="<?php echo $tri_champ ?>" />
				<input type="hidden" name="tri_ordre" value="<?php echo $tri_ordre ?>" />
			</form>
			<table class="boiteaction">
				<caption><?php echo $lang_articles_liste; ?></caption>
				<tr>
					<th><a href="javascript:ecrit_ordre_et_post('frm_tri','article')"><?php echo $lang_article; ?></a></th>
					<?php
					if ($conf_xml->categorie->categorie_active=="oui") { ?>
						<th><a href="javascript:ecrit_ordre_et_post('frm_tri','categorie')"><?php echo $lang_categorie ?></a></th>
					<?php } ?>
					<th><a href="javascript:ecrit_ordre_et_post('frm_tri','prix_htva')"><?php echo $lang_htva; ?></a></th>
					<th><a href="javascript:ecrit_ordre_et_post('frm_tri','taux_tva')"><?php echo $lang_taux_tva; ?></a></th>
					<th><a href="javascript:ecrit_ordre_et_post('frm_tri','uni')"><?php echo $lang_unite; ?></a></th>
					<?php
						if($conf_xml->stock->stock_actif=="oui") { ?>
					<th><a href="javascript:ecrit_ordre_et_post('frm_tri','stock')"><?php echo $lang_stock; ?></a></th>
					<th><a href="javascript:ecrit_ordre_et_post('frm_tri','stomin')"><?php echo $lang_stomin; ?></a></th>
					<th><a href="javascript:ecrit_ordre_et_post('frm_tri','stomax')"><?php echo $lang_stomax; ?></a></th>
					<?php } ?>
					<th colspan="2"><?php echo $lang_action; ?></th>
				</tr>
				<?php
				$nombre="1";
				while($data = mysql_fetch_array($req))
				{
					$article = $data['article'];
					$article_html=addslashes($article);
					$article = htmlentities($article, ENT_QUOTES,'UTF-8');
					$cat = $data['categorie'];
					$cat = htmlentities($cat, ENT_QUOTES,'UTF-8');
					$num =$data['num'];
					$prix = $data['prix_htva'];
					$tva = $data['taux_tva'];
					$uni = $data['uni'];
					$stock = $data['stock'];
					$min = $data['stomin'];
					$max = $data['stomax'];
					if ($stock < $min || $stock > $max  ) {
						$stock="$stock";
					}
					$nombre = $nombre +1;
					if($nombre & 1) {
						$line="0";
					} else {
						$line="1";
					}
				?>
				<tr class="texte<?php echo"$line" ?>" >
					<td class="highlight"><?php echo "$article"; ?></td>
					<?php
					if ($conf_xml->categorie->categorie_active=="oui") { ?>
						<td class="highlight"><?php echo $cat; ?></td>
					<?php } ?>
					<td class="highlight" style="text-align:right;padding-right:5px"><?php echo montant_financier ($prix); ?></td>
					<td class="highlight" style="text-align:right;padding-right:5px"><?php echo montant_taux($tva) ?></td>
					<td class="highlight"><?php echo $uni; ?></td>
					<?php
					if($conf_xml->stock->stock_actif=="oui"){?>
						<td class="highlight" style="text-align:right;padding-right:5px"><?php echo $stock; ?></td>
						<td class="highlight" style="text-align:right;padding-right:5px"><?php echo $min; ?></td>
						<td class="highlight" style="text-align:right;padding-right:5px"><?php echo $max; ?></td>
					<?php } ?>
					<td class="highlight">
						<a href='article_edit.php?article=<?php echo $num; ?>'><img border="0" alt="<?php echo $lang_editer; ?>" src="image/edit.png" /></a>
					</td>
					<td class="highlight">
						<a href="article_delete.php?article=<?php echo $num; ?>" onclick="return confirmDelete('<?php echo get_lang("Confirmez vous la suppression de l\'article : ") . $article ?>')"><img border="0" alt="<?php echo $lang_suprimer; ?>" src="image/delete.png" /></a>
					</td>
				</tr>
			<?php
			}
			?>
			</table>
			</center>
		</td>
	</tr>
	<tr>
		<td>
			<?php
			require_once("include/bas.php");
			?>
		</td>
	</tr>

</table>
</body>
</html>
